package com.guaoran.algorithm.binary.tree.order;

import com.guaoran.algorithm.entity.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Auth gucheng
 * @Desc 二叉树后序遍历 左 - 右 - 根
 * https://leetcode.com/problems/binary-tree-postorder-traversal/
 * @Date 2021/1/19 上午11:52
 */
public class PostOrderTraversal {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode right = new TreeNode(2);
        right.left = new TreeNode(3);
        root.right = right;
        System.out.println(postOrderTraversalDemo(root));
        System.out.println(postOrderTraversal(root));
        System.out.println(postOrderTraversal1(root));

    }


    private static List<Integer> postOrderTraversal(TreeNode root){
        List<Integer> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode current = null;
        TreeNode prev = null;
        while(!stack.isEmpty()){
            current = stack.peek();
            if(prev == null || prev.left == current || prev.right == current){
                if(current.left != null){
                    stack.push(current.left);
                } else if (current.right != null) {
                    stack.push(current.right);
                }
            }else if (current.left == prev){
                if(current.right != null){
                    stack.push(current.right);
                }
            }else {
                result.add(current.val);
                stack.pop();
            }
            prev = current;
        }
        return result;
    }

    /**
     * 修改引用关系的方法
     * @param root
     * @return
     */
    private static List<Integer> postOrderTraversal1(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root == null){
            return result;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node.left == null && node.right == null){
                result.add(stack.pop().val);
                continue;
            }
            if(node.right != null){
                stack.push(node.right);
                node.right = null;
            }
            if(node.left != null){
                stack.push(node.left);
                node.left = null;
            }

        }
        return result;
    }


    /**
     * 递归方法
     * @param root
     * @return
     */
    public static List<Integer> postOrderTraversalDemo(TreeNode root) {
        List<Integer> result = new ArrayList();
        if(root == null){
            return result;
        }

        List<Integer> left = postOrderTraversalDemo(root.left);
        List<Integer> right = postOrderTraversalDemo(root.right);
        result.addAll(left);
        result.addAll(right);
        result.add(root.val);
        return result;

    }
}
